package com.ruoyi.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
 
/**
 * 从数据源配置
 * 若需要配置更多数据源 , 直接在yml中添加数据源配置再增加相应的新的数据源配置类即可
 */
//@Configuration
//@MapperScan(basePackages  = ClusterDbConfig.PACKAGE , sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDbConfig {
     // 精确到 cluster 目录，以便跟其他数据源隔离
    static final String PACKAGE = "com.edu.study.base.mapper.cluster";
    private static final String MAPPER_LOCATION = "classpath:mapper/cluster/**/*.xml";
    private static final String DOMAIN_PACKAGE = "com.edu.study.base";
 
    @Value("${spring.datasource.url2}")
    private String dbUrl2;
 
    @Value("${spring.datasource.username2}")
    private String username2;
 
    @Value("${spring.datasource.password2}")
    private String password2;
 
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;
    
    @Autowired
    private Environment env;
 
    
 
    @Bean(name="clusterDataSource")   //声明其为Bean实例
    public DataSource clusterDataSource() {
        DruidDataSource datasource = new DruidDataSource();
 
        datasource.setUrl(dbUrl2);
        datasource.setUsername(username2);
        datasource.setPassword(password2);
        datasource.setDriverClassName(driverClassName);
        return datasource;
    }
 
    @Bean(name = "clusterTransactionManager")
    public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager(clusterDataSource());
    }
 
    @Bean(name = "clusterSqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource culsterDataSource)
            throws Exception {
        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();// new SqlSessionFactoryBean();
        sessionFactory.setDataSource(culsterDataSource);
        //添加一行如下代码
        //VFS.addImplClass(SpringBootVFS.class);
        //sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                //.getResources(ClusterDbConfig.MAPPER_LOCATION));
        //sessionFactory.setTypeAliasesPackage(DOMAIN_PACKAGE);
        
        String typeAliasesPackage = MyBatisConfig.setTypeAliasesPackage(DOMAIN_PACKAGE);
        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
        sessionFactory.setMapperLocations(MyBatisConfig.resolveMapperLocations(StringUtils.split(MAPPER_LOCATION, ",")));
        
        String configLocation = env.getProperty("mybatis.configLocation"); 
        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
        //mybatis 数据库字段与实体类属性驼峰映射配置
        //sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }
}
 
